package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.BaseEntity;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分类管理service
 * @author Xiao
 * @date 2021/9/24
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    /**
     * 新增分类
     * @return
     */
    @Override
    public boolean saveCategory(Category category) {
        return this.save(category);
    }
    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Category> getCategoryByPage(Integer page, Integer pageSize) {
        //分页构造器
        Page<Category> categoryPage = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //1.根据排序字段(sort)排序   2.根据修改时间字段(update_time)排序
        wrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        return this.page(categoryPage,wrapper);
    }
    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @Override
    public boolean removeCategoryById(Long id) {
        return this.removeById(id);
    }
    /**
     * 删除分类前判断是否关联了其他表
     * @param id
     */
    @Override
    public void removeCategoryByIdPlus(Long id) {
        //调用DishService和SetmealService的查询方法
        int countDish = dishService.getCountByCategoryId(id);
        int countSetmeal = setmealService.getCountByCategoryId(id);
        //判断当前分类下是否关联了菜品/套餐，如果关联了，抛出异常，否则执行删除方法
        if (countDish > 0) {
            throw new CustomException("删除失败，当前分类下关联了菜品！");
        }
        if (countSetmeal > 0) {
            throw new CustomException("删除失败，当前分类下关联了套餐！");
        }
        this.removeById(id);
    }

    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    @Override
    public Category queryCategoryById(Long id) {
        return this.getById(id);
    }

    /**
     * 编辑分类
     * @param category
     * @return
     */
    @Override
    public boolean editCategory(Category category) {
        return this.updateById(category);
    }

    /**
     * 根据条件获取分类数据
     * @param category
     * @return
     */
    @Override
    public List<Category> getCategoryList(Category category) {
        if (category != null) {
        Integer type = category.getType();
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //根据type查询
        wrapper.eq(type != null,Category::getType,type);
        //根据sort和updateTime字段排序
        wrapper.orderByAsc(Category::getSort).orderByDesc(BaseEntity::getUpdateTime);
        return this.list(wrapper);
        }
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort).orderByDesc(BaseEntity::getUpdateTime);
        return this.list(wrapper);
    }
}